import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Dashboard from '@/components/Dashboard'
import Index from '@/components/welcome/index.vue'

import BookList from '@/components/role/list'

import UserList from '@/components/user/list'
import UserChangePwd from '@/components/user/changepwd'
import UserProfile from '@/components/user/profile'

import MenuList from '@/components/menu/list'

import FileList from '../components/file/List.vue'

import LogList from '../components/log/List'


// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = resolve => require(['@/components/Login'], resolve)

Vue.use(Router)

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/index',
      name: '首页',
      component: Index
    },
    {
      path: '/login',
      name: '登录',
      component: Login
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      redirect: '/dashboard',
      leaf: true, // 只有一个节点
      menuShow: true,
      iconCls: 'fa fa-home', // 图标样式class
      children: [
        {path: '/dashboard', component: Dashboard, name: '开始', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '用户管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'fa fa-user', // 图标样式class
      children: [
        {path: '/admin/user', component: UserList, name: '用户列表', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '菜单管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'fa fa-server', // 图标样式class
      children: [
        {path: '/admin/menu', component: MenuList, name: '菜单列表', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '角色管理',
      menuShow: true,
      leaf: true,
      iconCls: 'fa fa-group',
      children: [
        {path: '/admin/role', component: BookList, name: '用户角色', menuShow: true},
      ]
    },

    {
      path: '/',
      component: Home,
      name: '文件管理',
      menuShow: true,
      leaf: true,
      iconCls: 'fa fa-group',
      children: [
        {path: '/cms/file', component: FileList, name: '文件', menuShow: true},
				{path: '/sys/oss', component: () => import('@/views/sys/os'), name: 'OSS', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '日志管理',
      menuShow: true,
      leaf: true,
      iconCls: 'fa fa-group',
      children: [
        {path: '/base/log', component: LogList, name: '日志', menuShow: true},
      ]
    },
    {
      path: '/',
      component: Home,
      name: '设置',
      menuShow: true,
      iconCls: 'iconfont icon-setting1',
      children: [
        {path: '/user/profile', component: UserProfile, name: '个人信息', menuShow: true},
        {path: '/user/changepwd', component: UserChangePwd, name: '修改密码', menuShow: true}
      ]
    },

    {
      path: '/',
      component: Home,
      name: '注册服务',
      menuShow: true,
      iconCls: 'iconfont icon-setting1',
      children: [
        {path: '/service',component: UserProfile,  redirect: 'http://baidu.com',target:'_blank',name: '注册', menuShow: true},
      ]
    },
		
		{
			path: '/',
			component: Home,
			name: '会员管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/user/user',  component: () => import('@/views/user/user'), name: '会员信息', menuShow: true},
				{path: '/user/address',  component: () => import('@/views/user/address'), name: '收货地址', menuShow: true},
				{path: '/user/collect',  component: () => import('@/views/user/collect'), name: '会员收藏', menuShow: true},
				{path: '/user/footprint',  component: () => import('@/views/user/footprint'), name: '会员足迹', menuShow: true},
				{path: '/user/history',  component: () => import('@/views/user/history'), name: '搜索历史', menuShow: true},
				{path: '/user/feedback',  component: () => import('@/views/user/feedback'), name: '意见反馈', menuShow: true},
			]
		},
		
		{
			path: '/',
			component: Home,
			name: '商场管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/mall/region',  component: () => import('@/views/mall/region'), name: '行政区域', menuShow: true},
				{path: '/mall/brand',  component: () => import('@/views/mall/brand'), name: '品牌制造商', menuShow: true},
				{path: '/mall/category',  component: () => import('@/views/mall/category'), name: '商品类目', menuShow: true},
				{path: '/mall/order',  component: () => import('@/views/mall/order'), name: '订单管理', menuShow: true},
				{path: '/mall/issue',  component: () => import('@/views/mall/issue'), name: '通用问题', menuShow: true},
				{path: '/mall/keyword',  component: () => import('@/views/mall/keyword'), name: '关键词', menuShow: true},
			]
		},
		
		{
			path: '/',
			component: Home,
			name: '商品管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/goods/list',  component: () => import('@/views/goods/list'), name: '商品列表', menuShow: true},
				{path: '/goods/create',  component: () => import('@/views/goods/create'), name: '商品上架', menuShow: true},
				{path: '/goods/edit',  component: () => import('@/views/goods/edit'), name: '商品编辑', menuShow: true},
				{path: '/goods/comment',  component: () => import('@/views/goods/comment'), name: '商品评论', menuShow: true},
			]
		},
		
		{
			path: '/',
			component: Home,
			name: '推广管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/promotion/ad',  component: () => import('@/views/promotion/ad'), name: '广告管理', menuShow: true},
				{path: '/promotion/coupon',  component: () => import('@/views/promotion/coupon'), name: '优惠券管理', menuShow: true},
				{path: '/promotion/couponDetail',  component: () => import('@/views/promotion/couponDetail'), name: '优惠券详情', menuShow: true},
				{path: '/promotion/topic',  component: () => import('@/views/promotion/topic'), name: '专题管理', menuShow: true},
				{path: '/promotion/grouponRule',  component: () => import('@/views/promotion/grouponRule'), name: '团购规则', menuShow: true},
				{path: '/promotion/grouponActivity',  component: () => import('@/views/promotion/grouponActivity'), name: '团购活动', menuShow: true},
			]
		},
		
		{
			path: '/',
			component: Home,
			name: '统计管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/stat/user',  component: () => import('@/views/stat/user'), name: '用户统计', menuShow: true},
				{path: '/stat/order',  component: () => import('@/views/stat/order'), name: '订单统计', menuShow: true},
				{path: '/stat/goods',  component: () => import('@/views/stat/goods'), name: '商品统计', menuShow: true}
			]
		},
		
		{
			path: '/',
			component: Home,
			name: '外链管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/link/link',  component: () => import('@/views/link/link'), name: '外链', menuShow: true},
			]
		},
		
		
		
		
		//////WMS
		{
			path: '/',
			component: Home,
			name: '进货管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/purchase/purchase',  component: () => import('@/wms/main'), name: '进货入库', menuShow: true},
				{path: '/purchase/return',  component: () => import('@/wms/main'), name: '退货出库', menuShow: true},
				{path: '/purchase/purchaseSearch',  component: () => import('@/wms/main'), name: '进货单据查询', menuShow: true},
				{path: '/purchase/returnSearch',  component: () => import('@/wms/main'), name: '退货单据查询', menuShow: true},
			]
		},
		{
			path: '/',
			component: Home,
			name: '销售管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/sale/saleout',  component: () => import('@/wms/main'), name: '销售出库', menuShow: true},
				{path: '/sale/salereturn',  component: () => import('@/wms/main'), name: '客户退货', menuShow: true},
				{path: '/sale/saleSearch',  component: () => import('@/wms/main'), name: '销售单据查询', menuShow: true},
				{path: '/sale/returnSearch',  component: () => import('@/wms/main'), name: '客户退货查询', menuShow: true},
			]
		},
		{
			path: '/',
			component: Home,
			name: '库存管理',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/stock/damage',  component: () => import('@/wms/main'), name: '商品报损', menuShow: true},
				{path: '/stock/overflow',  component: () => import('@/wms/main'), name: '商品报溢', menuShow: true},
				{path: '/stock/alarm',  component: () => import('@/wms/main'), name: '库存报警', menuShow: true},
				{path: '/stock/damageOverflowSearch',  component: () => import('@/wms/main'), name: '报损报溢查询', menuShow: true},
				{path: '/common/stockSearch-3',  component: () => import('@/wms/main'), name: '当前库存查询', menuShow: true}
			]
		},
		{
			path: '/',
			component: Home,
			name: '仓储统计',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/count/supplier',  component: () => import('@/wms/main'), name: '供应商统计', menuShow: true},
				{path: '/count/customer',  component: () => import('@/wms/main'), name: '客户统计', menuShow: true},
				{path: '/count/purchase',  component: () => import('@/wms/main'), name: '商品采购统计', menuShow: true},
				{path: '/count/sale',  component: () => import('@/wms/main'), name: '商品销售统计', menuShow: true},
				{path: '/count/saleDay',  component: () => import('@/wms/main'), name: '按日统计分析', menuShow: true},
				{path: '/count/saleMonth',  component: () => import('@/wms/main'), name: '按月统计分析', menuShow: true}
			]
		},
		{
			path: '/',
			component: Home,
			name: '基础资料',
			menuShow: true,
			iconCls: 'iconfont icon-setting1',
			children: [
				{path: '/basicData/supplier',  component: () => import('@/wms/main'), name: '供应商管理', menuShow: true},
				{path: '/basicData/customer',  component: () => import('@/wms/main'), name: '客户管理', menuShow: true},
				{path: '/basicData/goods',  component: () => import('@/wms/main'), name: '商品管理', menuShow: true},
				{path: '/basicData/stock',  component: () => import('@/wms/main'), name: '期初库存', menuShow: true},
				{path: '/power/log',  component: () => import('@/wms/main'), name: '系统日志', menuShow: true}
			]
		},
  ]
})


router.beforeEach((to, from, next) => {
  // console.log('to:' + to.path)
  if (to.path.startsWith('/login')) {
    window.localStorage.removeItem('access-token')
    //window.localStorage.removeItem('access-user')
    next()
  } else if (to.path.startsWith('/index')) {
    next()
  } else {
    //let user = JSON.parse(window.localStorage.getItem('access-token'))
    let user = window.localStorage.getItem('access-token');
    if (!user) {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

export default router
